Skip to content

Add Tensor.to_sparse() API for sparse COO tensor conversion#1525

Open
alinpahontu2912 wants to merge 2 commits intodotnet:mainfrom
alinpahontu2912:add-to-sparse-api
Open

Add Tensor.to_sparse() API for sparse COO tensor conversion#1525
alinpahontu2912 wants to merge 2 commits intodotnet:mainfrom
alinpahontu2912:add-to-sparse-api

Conversation

@alinpahontu2912
Copy link
Copy Markdown
Member

Add to_sparse() and to_sparse(int sparse_dim) methods to convert dense tensors to sparse COO format. This enables GCN and GAT graph neural network examples that require sparse matrix operations.

Changes across all 4 binding layers:

  • THSTensor.h: declarations
  • THSTensor.cpp: implementations
  • LibTorchSharp.THSTensor.cs: P/Invoke
  • Tensor.cs: managed C# methods

Copy link
Copy Markdown
Contributor

Copilot AI left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Pull request overview

This PR adds support for converting dense tensors to sparse COO (Coordinate) format through the to_sparse() API, which is essential for implementing graph neural networks (GCN and GAT) that rely on sparse matrix operations. The implementation follows the standard TorchSharp pattern of adding bindings across all four layers: C++ native code, P/Invoke declarations, and managed C# methods.

Changes:

  • Added two overloads of to_sparse(): a parameterless version and one accepting a sparse dimension parameter
  • Implemented full binding chain from managed C# through P/Invoke to C++ native LibTorch calls
  • Documentation added for both method overloads

Reviewed changes

Copilot reviewed 4 out of 4 changed files in this pull request and generated 2 comments.

File Description
src/TorchSharp/Tensor/Tensor.cs Added managed C# methods to_sparse() and to_sparse(int sparse_dim) with XML documentation
src/TorchSharp/PInvoke/LibTorchSharp.THSTensor.cs Added P/Invoke declarations for both native methods
src/Native/LibTorchSharp/THSTensor.h Added C++ function declarations for sparse tensor conversion
src/Native/LibTorchSharp/THSTensor.cpp Implemented C++ wrapper functions calling LibTorch's to_sparse() API

💡 Add Copilot custom instructions for smarter, more guided reviews. Learn how to get started.

Add to_sparse() and to_sparse(int sparse_dim) methods to convert dense
tensors to sparse COO format. This enables GCN and GAT graph neural
network examples that require sparse matrix operations.

Changes across all 4 binding layers:
- THSTensor.h: declarations
- THSTensor.cpp: implementations
- LibTorchSharp.THSTensor.cs: P/Invoke
- Tensor.cs: managed C# methods
Copy link
Copy Markdown
Contributor

Copilot AI left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Pull request overview

Copilot reviewed 4 out of 4 changed files in this pull request and generated no new comments.


💡 Add Copilot custom instructions for smarter, more guided reviews. Learn how to get started.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

3 participants